package models

import (
	"strings"
	"time"

	"github.com/jinzhu/gorm"
)

type CoinLog struct {
	ID            int64     `gorm:"column:id" json:"id"`
	CoinCount     int64     `gorm:"column:coin_count" json:"coin_count"`
	MemberId      int64     `gorm:"column:member_id" json:"member_id"`
	CreateTime    time.Time `gorm:"column:create_time" json:"create_time"`
	DeviceId      int64     `gorm:"column:device_id" json:"device_id"`
	StoreId       int64     `gorm:"column:store_id" json:"store_id"`
	MerchantId    int64     `gorm:"column:merchant_id" json:"merchant_id"`
	ModularId     int64     `gorm:"column:modular_id" json:"modular_id"`
	ModularType   int64     `gorm:"column:modular_type" json:"modular_type"`
	Type          int64     `gorm:"column:type" json:"type"`
	Coin          int64     `gorm:"column:coin" json:"coin"`
	Status        int64     `gorm:"column:status" json:"status"`
	OrderId       int64     `gorm:"column:order_id" json:"order_id"`
	OrderType     int64     `gorm:"column:order_type" json:"order_type"`
	InstallSite   int64     `gorm:"column:install_site" json:"install_site"`
	ChooseCount   int64     `gorm:"column:choose_count" json:"choose_count"`
	OrderNumber   int64     `gorm:"column:order_number" json:"order_number"`
	UpdateTime    time.Time `gorm:"column:update_time" json:"update_time"`
	DeviceAlias   string    `gorm:"column:device_alias" json:"device_alias"`
	Balance       int64     `gorm:"column:balance" json:"balance"`
	MCoinCount    int64     `gorm:"column:m_coin_count" json:"m_coin_count"`
	MBalance      int64     `gorm:"column:m_balance" json:"m_balance"`
	RealCoinCount int64     `gorm:"column:real_coin_count" json:"real_coin_count"`
	Device        *Device   `json:"device,omitempty"`
	Store         *Store    `json:"store,omitempty"`
	Merchant      *Merchant `json:"merchant,omitempty"`
}

func (this *CoinLog) GetListByMemberID(
	where map[string]interface{},
	pagination map[string]interface{},
	table string,
) (
	coinLogs []CoinLog,
	count int,
) {
	db := Db
	db = this.Before(db, table)
	db = ParsePagination(db, pagination)
	db = ParseWhere(db, where)
	db.
		Table("coin_log").
		Count(&count).
		Find(&coinLogs)

	return
}
func (this *CoinLog) GetById(
	where map[string]interface{},
	table string,
) (coinLog CoinLog) {

	db := Db
	db = this.Before(db, table)
	db = ParseWhere(db, where)
	db.Find(&coinLog)
	return
}
func (this *CoinLog) Before(result *gorm.DB, table string) *gorm.DB {
	if strings.Contains(table, "device") {
		result = result.Preload("Device")
	}
	if strings.Contains(table, "store") {
		result = result.Preload("Store")
	}
	if strings.Contains(table, "merchant") {
		result = result.Preload("Merchant")
	}
	return result
}
